<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   会员中心
 */
defined('IN_YuLin') || exit('NO PERMIT!');
$seo['title'] = '大众彩票-添加银行卡';
$pageTitle    = '添加银行卡';
$navTag       = 'member';
$page         = 'member/bindcard';

UserLogin();
$userInfo     = $_user;
// 用户绑定银行卡信息表
$bindBankTable = Table('user_bank');
// 支持银行信息表
$bankCateTab   = Table('web_bank');

if (IS_POST){
    $operation = NoBadStr($_POST['operation']);
    $data = array(
        'uid'       => $_uid,
        'bank'      => NoBadStr($_POST['bank']),
        'province'  => NoBadStr($_POST['province']),
        'city'      => NoBadStr($_POST['city']),
        'truename'  => NoBadStr($_POST['truename']),
        'bankcard'  => NoBadStr($_POST['bankcard']),
        'security'  => md5(NoBadStr($_POST['safepwd'])),
        'status'    => '1',
    );
    switch ($operation){
        case 'add':
            $data['dateline'] = time();
            $flag             = $db->exec('INSERT INTO '.$bindBankTable.CreateInsertSql($data));
            if ($flag){
                $redirect = U('member/bankcard');
                header('Location:' . $redirect);
            }
            $error = '操作失败，请重试';
            break;
        case 'edit':
            $id           = NoBadStr($_POST['id']);
            if (!$id){
                $error = '操作失败，请重试';
                break;
            }
            $sql          = 'UPDATE ' . $bindBankTable . ' SET ' . CreateUpdateSql($data) . ' WHERE id = ' . $id;
            $flag         = $db->exec($sql);
            if ($flag){
                $redirect = U('member/bankcard');
                header('Location:' . $redirect);
            }
            $error = '操作失败，请重试';
            break;
    }
    $tpl->display($page);
    return;
}

$bankCateSql   = 'SELECT id, name, tags FROM ' . $bankCateTab . ' WHERE status = 1';
$bankCateList  = $db->getall($bankCateSql);
$bankCateIdKey = array();
if ($bankCateList){
    foreach ($bankCateList as $bk => $bv){
        $bankCateIdKey[$bv['id']] = $bv;
    }
}
switch ($a){
    case 'edit':
        $id       = NoBadStr($_GET['id']);
        $sql      = 'SELECT id, uid, truename, bank, bankcard, province, city, security, dateline, status FROM ' . $bindBankTable . ' WHERE id = ' . $id . ' AND uid = ' . $_uid . ' AND status = 1';
        $bindInfo = $db->getrow($sql);
        break;
    default:
        break;
}

$tpl->display($page);